package br.ufpe.cin.rdfilter.util;

import java.util.ArrayList;
import java.util.List;

import br.ufpe.cin.rdfilter.model.Feedback;
import br.ufpe.cin.rdfilter.model.Manager;
import br.ufpe.cin.rdfilter.model.Query;

public class FeedbackManager {

	private Manager m;
	private Measure measure1;
	private Measure measure2;
	private List<String> terms;
	private double precision;
	private double recall;
	private double f_measure;
	private double benefit = 0.0;
	private int tp = 0;
	private int fp = 0;
	private int fn = 0;
	private List<Feedback> feedbackList;
	
	public FeedbackManager(List<String> terms, List<Feedback> feedbackList){
		m = new Manager();
		measure1 = new Measure();
		measure2 = new Measure();
		this.terms=terms;
		this.feedbackList=feedbackList;
		
	}
	
	public Double calculateBenefit(){
		int i=0;
		int count = 0;
		Boolean signal=false;
		String temp="";
//		feedbackList = m.getFeedback();
		List<Feedback> f_temp= new ArrayList<Feedback>();;
		Feedback feedback_temp = new Feedback();
		System.out.println("Fazendo a inferencia...");
		if(!terms.isEmpty()){
			while(i<feedbackList.size()){
				signal=false;
				int j=0;

				while(j<terms.size() && !signal){
					temp=feedbackList.get(i).getTerm();
					if(StringSimilarity.isSimilarity(temp, terms.get(j), 0.95)){
						if(feedbackList.get(i).getType().equals("True Positive") ||
								feedbackList.get(i).getType().equals("False Negative")){
							//add new annotation with TP
							feedback_temp=feedbackList.get(i);
							feedback_temp.setType("True Positive");
							f_temp.add(feedback_temp);
						}
						//false positive
						else{
								//add new annotation with FP
							feedback_temp=feedbackList.get(i);
							feedback_temp.setType("False Positive");
							f_temp.add(feedback_temp);
						}
						
						signal=true;
					}
					j++;
				}
				if(!signal && feedbackList.get(i).getType().equals("False Negative")) count++;
				i++;
			}
			
			
			measure1.calculate(f_temp);
			measure1.setFn(count);
			
			measure2.calculate(feedbackList);
			
			benefit = measure1.getF_measure()/measure2.getF_measure();
		}
		else{System.out.println("Dataset vazio!"); benefit = 0.0;}
		System.out.println("Cabou a inferencia!!! Misericordia!");
		System.out.println("Teste Beneficio --- " +benefit);
		if(benefit>10.0)
			benefit = 10.0;
		
		return benefit;
	}

}
